package com.yzs.Interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yzs.common.bean.R;
import com.yzs.service.Impl.RedisServiceImpl;
import com.yzs.service.RedisService;
import com.yzs.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    JwtUtil jwtUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (ObjectUtils.isEmpty(token)) {
            noToken(response,R.error(401,"用户未登录，请重新登录！"));
            return false;
        }
        Jws<Claims> jwsAdminInfo = jwtUtil.getTokenAdminInfo(token);
        Jws<Claims> jwsUserInfo = jwtUtil.getTokenUserInfo(token);
        String userSubject = "";
        Integer uid = null;
        String adminSubject = "";
        Integer aid = null;
        if(!ObjectUtils.isEmpty(jwsAdminInfo)){
            adminSubject = jwsAdminInfo.getBody().getSubject();
            aid = (Integer) jwsAdminInfo.getBody().get("id");
        }
        if(!ObjectUtils.isEmpty(jwsUserInfo)){
            userSubject = jwsUserInfo.getBody().getSubject();
            uid = (Integer) jwsUserInfo.getBody().get("id");
        }
        String uToken = (String) redisService.get(userSubject + "_" + uid);
        String aToken = (String) redisService.get(adminSubject + "_" + aid);
        if (uToken == null && aToken == null){
            noToken(response,R.error(401,"token错误，请重新登录！"));
            return false;
        } else if (uToken != null) {
            redisService.expire(userSubject + "_" + uid,1000 * 60 * 60 * 24);
        }else if(aToken != null){
            redisService.expire(adminSubject + "_" + aid,1000 * 60 * 60 * 24);
        }
        return true;
    }

    private void noToken(HttpServletResponse response, R<Object> error) throws IOException {
        response.setContentType("text/application;charset=utf-8");
        PrintWriter writer = response.getWriter();
        String s = objectMapper.writeValueAsString(error);
        writer.write(s);
        writer.flush();
        writer.close();
    }


//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
}
